﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Honestysoft.BaseObject;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.Financing.Model;
using System.Drawing;
using System.Collections.Generic;
using Honestysoft.Workflow.BusinessFacade;

namespace Honestysoft.Financing.WebUI.Module.Audit
{
    public partial class AuditTable_C : BasePage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                BindControl();
                BindWF();
                Bind();
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            WFButtons1.AfterCall += new WorkFlow_Control_WFButtons.AfterCallEventHandler(WFButtons1_AfterCall);
            btnReceive.Click += new EventHandler(btnReceive_Click);
        }

        void btnReceive_Click(object sender, EventArgs e)
        {
            BindControl();
            BindWF();
            Bind();
        }

        void WFButtons1_AfterCall(Workflow.Model.WFReturn pWFReturn)
        {
            BindControl();
            BindWF();
            Bind();
        }

        protected string FormatRecDate(DateTime date)
        {
            string ret = string.Format("{0:yyyy年M月d日}（周", date);

            switch (DateTime.Today.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    ret += "一";
                    break;
                case DayOfWeek.Tuesday:
                    ret += "二";
                    break;
                case DayOfWeek.Wednesday:
                    ret += "三";
                    break;
                case DayOfWeek.Thursday:
                    ret += "四";
                    break;
                case DayOfWeek.Friday:
                    ret += "五";
                    break;
                case DayOfWeek.Saturday:
                    ret += "六";
                    break;
                case DayOfWeek.Sunday:
                    ret += "日";
                    break;
                default:
                    break;
            }
            ret += "）";
            return ret;
        }
        private DataTable dtYesNo = null;
        private void BindRadioList(RadioButtonList rbl)
        {
            rbl.DataSource = dtYesNo;
            rbl.DataTextField = "Text";
            rbl.DataValueField = "Value";
            rbl.DataBind();
            rbl.SelectedIndex = 0;
        }
        protected Bu_AuditTable theTable = null;

        private void BindControl()
        {
            dtYesNo = new DataTable();
            dtYesNo.Columns.Add("Text", typeof(string));
            dtYesNo.Columns.Add("Value", typeof(string));
            DataRow dr = dtYesNo.NewRow();
            dr["Text"] = "有  ";
            dr["Value"] = "1";
            dtYesNo.Rows.Add(dr);
            dr = dtYesNo.NewRow();
            dr["Text"] = "无";
            dr["Value"] = "0";
            dtYesNo.Rows.Add(dr);

            BindRadioList(rblPD1);
            BindRadioList(rblPD2);
            BindRadioList(rblPD3);
            BindRadioList(rblPD4);
            BindRadioList(rblPD5);
            BindRadioList(rblPD6);
            BindRadioList(rblPD7);
            BindRadioList(rblPD8);
            BindRadioList(rblPD9);
            BindRadioList(rblPD10);

            BindRadioList(rblZD1);
            BindRadioList(rblZD2);
            BindRadioList(rblZD3);
            BindRadioList(rblZD4);

            BindRadioList(rblCD1);
            BindRadioList(rblCD2);
            BindRadioList(rblCD3);
            BindRadioList(rblCD4);
            BindRadioList(rblCD5);
            BindRadioList(rblCD6);
            BindRadioList(rblCD7);
            BindRadioList(rblCD8);
            BindRadioList(rblCD9);
        }

        private void BindWF()
        {
            WFButtons1.BillCode = ((int)EnumItem.WFBill.AuditTable).ToString();
            WFButtons1.EmployeeID = UserLoginer.LoginUser.ID;
            WFButtons1.BuDataID = Request.QueryString["ID"];
            WFButtons1.IsCallBackPage = true;
            WFButtons1.IsHiddenBtnStart = true;
            WFButtons1.BindWFControl();
        }

        private void LockPage()
        {
            isLock = true;
            //WFButtons1.Visible = false;
            btnSave.Enabled = false;
            numCurAmount.Enabled = false;
            numTAmount_C_A.Enabled = false;
            numAmount_C.Enabled = false;
            rblCD1.Enabled = false;
            rblCD2.Enabled = false;
            rblCD3.Enabled = false;
            rblCD4.Enabled = false;
            rblCD5.Enabled = false;
            rblCD6.Enabled = false;
            rblCD7.Enabled = false;
            rblCD8.Enabled = false;
            rblCD9.Enabled = false;
            rblPD1.Enabled = false;
            rblPD2.Enabled = false;
            rblPD3.Enabled = false;
            rblPD4.Enabled = false;
            rblPD5.Enabled = false;
            rblPD6.Enabled = false;
            rblPD7.Enabled = false;
            rblPD8.Enabled = false;
            rblPD9.Enabled = false;
            rblPD10.Enabled = false;

            rblZD1.Enabled = false;
            rblZD2.Enabled = false;
            rblZD3.Enabled = false;
            rblZD4.Enabled = false;

            //txtCD.Enabled = false;
            //txtCDOpinion.Enabled = false;
            //txtForCPriceOpinion.Enabled = false;
            //txtForLoanOpinion.Enabled = false;
            //txtForPlanOpinion.Enabled = false;
            //txtForPPriceOpinion.Enabled = false;
            //txtForProgress.Enabled = false;
            //txtForProgressOpinion.Enabled = false;
            //txtPD1.Enabled = false;
            //txtPD1Opinion.Enabled = false;
            //txtPD2.Enabled = false;
            //txtPD2Opinion.Enabled = false;
            //txtZD.Enabled = false;
            //txtZDOpinion.Enabled = false;
        }

        protected int yearCount = 0;
        protected string yearString = "";

        bool isLock = false;

        protected string GetIsLock()
        {
            if (isLock)
            {
                return "  style='display:none'";
            }
            return "";
        }
        protected string _BuStatus = "";
        protected string _SwfFilePath = "";
        protected string _Code = "";
        protected string _BatchNo = "";
        /// <summary>
        /// 绑定页面
        /// </summary>
        private void Bind()
        {
            QueryParameters query = new QueryParameters();
            query.Add("TableID", Request.QueryString["ID"], QueryParameters.OperatorType.Equal);
            List<Bu_BatchRec> lsBatchRec = new BaseBusinessLogic<Bu_BatchRec>(Globals.CurrentConnStringName).GetEntitys(query);
            if (lsBatchRec == null || lsBatchRec.Count != 1)
            {
                lblInfo.Text = "找不到作业表对应的回执！";
                LockPage();
                return;
            }
            btnViewReceive.Attributes.Add("onclick", "javascript:doConfirm('" + lsBatchRec[0].ID + "');return false;");
            btnReceive.Attributes.Add("onclick", "javascript:return doConfirm('" + lsBatchRec[0].ID + "');");
            btnReceive.Enabled = false;
            btnViewReceive.Enabled = false;

            theTable = new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).GetAuditTable(Request.QueryString["ID"]);
            if (theTable == null)
            {
                lblInfo.Text = "找不到符合条件的作业表！";
                LockPage();
                return;
            }
            hidBuStatus.Value = theTable.BuStatus;
            _BuStatus = theTable.BuStatus;
            _Code = theTable.Code;
            _BatchNo = theTable.BatchNo.ToString();
            _SwfFilePath = Globals.Home + "Reports/" + theTable.BatchNo + "/" + theTable.Code + ".swf";
            //产生此表格时，默认的初审金额，复核金额为回执的金额，初审完成后，复核也更改为初审金额，最终以复核金额为准
            //如果当前是初审/复核，如果当前人员是审核人，则可以编辑
            if (!lsBatchRec[0].IsSend)
            {
                lblInfo.Text = "批次管理员未对当前作业表的进行派单，不能进行操作！";
                LockPage();
            }
            else
            {
                if (theTable.BuStatus == ((int)EnumItem.AuditTableStatus.Lock).ToString()
                || theTable.BuStatus == ((int)EnumItem.AuditTableStatus.NoRec).ToString()
                    || theTable.BuStatus == ((int)EnumItem.AuditTableStatus.Output).ToString())
                {
                    numCurAmount.BackColor = Color.Transparent;
                    numTAmount_C_A.BackColor = Color.Transparent;
                    numCurAmount.Enabled = false;
                    numTAmount_C_A.Enabled = false;
                    btnSave.Enabled = false;
                    numCurAmount.Text = theTable.Amount_S.ToString("0.00");
                    numAmount_C.Text = theTable.Amount_C_S.ToString("0.00");
                    numTAmount_C_A.Text = theTable.TAmount_C_A_S.ToString("0.00");
                    numTAmount_C_A.Text = "0";
                    LockPage();
                    if (theTable.BuStatus == ((int)EnumItem.AuditTableStatus.NoRec).ToString())
                    {
                        //未签收，当前用户必须是派单的人才可以签收
                        if (lsBatchRec[0].ToUserID == UserLoginer.LoginUser.ID)
                        {
                            lblInfo.Text = "请先进行执行作业表签收，然后开始审核作业表！";
                            btnReceive.Enabled = true;
                        }
                        else
                        {
                            lblInfo.Text = "对不起，你不是当前作业表的签收人，不能进行签收！";
                        }
                    }
                    else if (theTable.BuStatus == ((int)EnumItem.AuditTableStatus.Lock).ToString())
                    {
                        lblInfo.Text = "当前作业表已锁定！";
                        btnViewReceive.Enabled = true;
                    }
                    else if (theTable.BuStatus == ((int)EnumItem.AuditTableStatus.Output).ToString())
                    {
                        lblInfo.Text = "当前作业表已输出！";
                        btnViewReceive.Enabled = true;
                    }
                }
                else
                {
                    btnViewReceive.Enabled = true;

                    if (theTable.BuStatus == ((int)EnumItem.AuditTableStatus.FAppr).ToString())//初审，与回执比较
                    {
                        if (theTable.IsInputAmount_F)
                        {
                            numCurAmount.Text = theTable.Amount_F.ToString("0.00");
                        }
                        else
                        {
                            numCurAmount.Text = "0";
                        }
                        if (theTable.IsInputTAmount_C_A_F)
                        {
                            numTAmount_C_A.Text = theTable.TAmount_C_A_F.ToString("0.00");
                        }
                        else
                        {
                            numTAmount_C_A.Text = "0";
                        }
                        if (theTable.IsInputAmount_C_F)
                        {
                            numAmount_C.Text = theTable.Amount_C_F.ToString("0.00");
                        }
                        else
                        {
                            numAmount_C.Text = "0";
                        }
                        numAmount_C.Attributes.Add("onchange", "javascript:confirmAmount3('" + theTable.Amount_C.ToString("0.00") + "','0');");
                        numCurAmount.Attributes.Add("onchange", "javascript:confirmAmount('" + theTable.Amount_DocRec.ToString("0.00") + "','0');");
                    }
                    else if (theTable.BuStatus == ((int)EnumItem.AuditTableStatus.SAppr).ToString())//复审，与初审比较，前期该合同项下累计拨付金额也需要比较
                    {
                        if (theTable.IsInputAmount_S)
                        {
                            numCurAmount.Text = theTable.Amount_S.ToString("0.00");
                        }
                        else
                        {
                            numCurAmount.Text = "0";
                        }
                        if (theTable.IsInputTAmount_C_A_S)
                        {
                            numTAmount_C_A.Text = theTable.TAmount_C_A_S.ToString("0.00");
                        }
                        else
                        {
                            numTAmount_C_A.Text = "0";
                        }
                        if (theTable.IsInputAmount_C_S)
                        {
                            numAmount_C.Text = theTable.Amount_C_S.ToString("0.00");
                        }
                        else
                        {
                            numAmount_C.Text = "0";
                        }
                        numAmount_C.Attributes.Add("onchange", "javascript:confirmAmount3('" + theTable.Amount_C_F.ToString("0.00") + "','1');");
                        numCurAmount.Attributes.Add("onchange", "javascript:confirmAmount('" + theTable.Amount_F.ToString("0.00") + "','1');");
                        numTAmount_C_A.Attributes.Add("onchange", "javascript:confirmAmount2('" + theTable.TAmount_C_A_F.ToString("0.00") + "');");
                    }
                    WFApply.Tracker ITrack = new WFApply.Tracker();
                    if (!ITrack.IsCanTransact(UserLoginer.LoginUser.ID, ((int)EnumItem.WFBill.AuditTable).ToString(), theTable.ID))
                    {
                        if (theTable.BuStatus == ((int)EnumItem.AuditTableStatus.FAppr).ToString())
                        {
                            #region 应该显示何种金额
                            if (theTable.IsInputAmount_F)
                            {
                                numCurAmount.Text = theTable.Amount_F.ToString("0.00");
                            }
                            else
                            {
                                numCurAmount.Text = theTable.Amount_DocRec.ToString("0.00");
                            }
                            if (theTable.IsInputTAmount_C_A_F)
                            {
                                numTAmount_C_A.Text = theTable.TAmount_C_A_F.ToString("0.00");
                            }
                            else
                            {
                                numTAmount_C_A.Text = "0";
                            }
                            if (theTable.IsInputAmount_C_F)
                            {
                                numAmount_C.Text = theTable.Amount_C_F.ToString("0.00");
                            }
                            else
                            {
                                numAmount_C.Text = theTable.Amount_C.ToString("0.00");
                            }
                            #endregion
                            lblInfo.Text = "对不起，你不是当前作业表的初审处理者，不能进行操作！";
                        }
                        else if (theTable.BuStatus == ((int)EnumItem.AuditTableStatus.SAppr).ToString())
                        {
                            #region 应该显示何种金额
                            if (theTable.IsInputAmount_S)
                            {
                                numCurAmount.Text = theTable.Amount_S.ToString("0.00");
                            }
                            else
                            {
                                if (theTable.IsInputAmount_F)
                                {
                                    numCurAmount.Text = theTable.Amount_F.ToString("0.00");
                                }
                                else
                                {
                                    numCurAmount.Text = theTable.Amount_DocRec.ToString("0.00");
                                }
                            }
                            if (theTable.IsInputTAmount_C_A_S)
                            {
                                numTAmount_C_A.Text = theTable.TAmount_C_A_S.ToString("0.00");
                            }
                            else
                            {
                                if (theTable.IsInputTAmount_C_A_F)
                                {
                                    numTAmount_C_A.Text = theTable.TAmount_C_A_F.ToString("0.00");
                                }
                                else
                                {
                                    numTAmount_C_A.Text = "0";
                                }
                            }
                            if (theTable.IsInputAmount_C_S)
                            {
                                numAmount_C.Text = theTable.Amount_C_S.ToString("0.00");
                            }
                            else
                            {
                                if (theTable.IsInputAmount_C_F)
                                {
                                    numAmount_C.Text = theTable.Amount_C_F.ToString("0.00");
                                }
                                else
                                {
                                    numAmount_C.Text = theTable.Amount_C.ToString("0.00");
                                }
                            }
                            #endregion
                            lblInfo.Text = "对不起，你不是当前作业表的复核处理者，不能进行操作！";
                        }
                        LockPage();
                    }
                }
            }
            numAmount_C.Attributes.Add("onfocus", "javascript:this.select();");
            numCurAmount.Attributes.Add("onfocus", "javascript:this.select();");
            numTAmount_C_A.Attributes.Add("onfocus", "javascript:this.select();");
            rptCurOther.DataSource = theTable.CurOther_C;
            rptCurOther.DataBind();

            rptOtherProject.DataSource = theTable.CurOther_Split;
            rptOtherProject.DataBind();

            rptTAmount_C_NA.DataSource = theTable.PastTime_C_NA;
            rptTAmount_C_NA.DataBind();
            rptCurOtherP.DataSource = theTable.CurOther_P;
            rptCurOtherP.DataBind();
            rptTAmount_P_NA.DataSource = theTable.PastTime_P_NA;
            rptTAmount_P_NA.DataBind();
            yearCount = theTable.YearPlans.Count;
            for (int i = 0; i < yearCount; i++)
            {
                if (yearString != "") { yearString += "+"; }
                yearString += "P" + (i + 1).ToString();
            }
            rptYearPlan.DataSource = theTable.YearPlans;
            rptYearPlan.DataBind();

            rblPD1.SelectedValue = theTable.AuditCase.IsPD1 ? "1" : "0";
            rblPD2.SelectedValue = theTable.AuditCase.IsPD2 ? "1" : "0";
            rblPD3.SelectedValue = theTable.AuditCase.IsPD3 ? "1" : "0";
            rblPD4.SelectedValue = theTable.AuditCase.IsPD4 ? "1" : "0";
            rblPD5.SelectedValue = theTable.AuditCase.IsPD5 ? "1" : "0";
            rblPD6.SelectedValue = theTable.AuditCase.IsPD6 ? "1" : "0";
            rblPD7.SelectedValue = theTable.AuditCase.IsPD7 ? "1" : "0";
            rblPD8.SelectedValue = theTable.AuditCase.IsPD8 ? "1" : "0";
            rblPD9.SelectedValue = theTable.AuditCase.IsPD9 ? "1" : "0";
            rblPD10.SelectedValue = theTable.AuditCase.IsPD10 ? "1" : "0";

            rblCD1.SelectedValue = theTable.AuditCase.IsCD1 ? "1" : "0";
            rblCD2.SelectedValue = theTable.AuditCase.IsCD2 ? "1" : "0";
            rblCD3.SelectedValue = theTable.AuditCase.IsCD3 ? "1" : "0";
            rblCD4.SelectedValue = theTable.AuditCase.IsCD4 ? "1" : "0";
            rblCD5.SelectedValue = theTable.AuditCase.IsCD5 ? "1" : "0";
            rblCD6.SelectedValue = theTable.AuditCase.IsCD6 ? "1" : "0";
            rblCD7.SelectedValue = theTable.AuditCase.IsCD7 ? "1" : "0";
            rblCD8.SelectedValue = theTable.AuditCase.IsCD8 ? "1" : "0";
            rblCD9.SelectedValue = theTable.AuditCase.IsCD9 ? "1" : "0";

            rblZD1.SelectedValue = theTable.AuditCase.IsZD1 ? "1" : "0";
            rblZD2.SelectedValue = theTable.AuditCase.IsZD2 ? "1" : "0";
            rblZD3.SelectedValue = theTable.AuditCase.IsZD3 ? "1" : "0";
            rblZD4.SelectedValue = theTable.AuditCase.IsZD4 ? "1" : "0";

            txtCD.Text = theTable.AuditCase.CD;
            txtCDOpinion.Text = theTable.AuditCase.CDOpinion;
            txtForCPriceOpinion.Text = theTable.AuditCase.ForCPriceOpinion;
            txtForLoanOpinion.Text = theTable.AuditCase.ForLoanOpinion;
            txtForPlanOpinion.Text = theTable.AuditCase.ForPlanOpinion;
            txtForPPriceOpinion.Text = theTable.AuditCase.ForPPriceOpinion;
            txtForProgress.Text = theTable.AuditCase.ForProgress;
            txtForProgressOpinion.Text = theTable.AuditCase.ForProgressOpinion;
            txtPD1.Text = theTable.AuditCase.PD1;
            txtPD1Opinion.Text = theTable.AuditCase.PD1Opinion;
            txtPD2.Text = theTable.AuditCase.PD2;
            txtPD2Opinion.Text = theTable.AuditCase.PD2Opinion;
            txtZD.Text = theTable.AuditCase.ZD;
            txtZDOpinion.Text = theTable.AuditCase.ZDOpinion;

            //如果是首次申请，则需要审核项目十项完整性，否则不需要
            rblPD1.Visible = false;
            rblPD2.Visible = false;
            rblPD3.Visible = false;
            rblPD4.Visible = false;
            rblPD5.Visible = false;
            rblPD6.Visible = false;
            rblPD7.Visible = false;
            rblPD8.Visible = false;
            rblPD9.Visible = false;
            rblPD10.Visible = false;
            if (theTable.SortNo == 1)
            {
                rblPD1.Visible = true;
                rblPD2.Visible = true;
                rblPD3.Visible = true;
                rblPD4.Visible = true;
                rblPD5.Visible = true;
                rblPD6.Visible = true;
                rblPD7.Visible = true;
                rblPD8.Visible = true;
                rblPD9.Visible = true;
                rblPD10.Visible = true;
            }

            if (theTable.BuStatus == ((int)EnumItem.AuditTableStatus.Output).ToString())
            {
                txtCD.Enabled = false;
                txtCDOpinion.Enabled = false;
                txtForCPriceOpinion.Enabled = false;
                txtForLoanOpinion.Enabled = false;
                txtForPlanOpinion.Enabled = false;
                txtForPPriceOpinion.Enabled = false;
                txtForProgress.Enabled = false;
                txtForProgressOpinion.Enabled = false;
                txtPD1.Enabled = false;
                txtPD1Opinion.Enabled = false;
                txtPD2.Enabled = false;
                txtPD2Opinion.Enabled = false;
                txtZD.Enabled = false;
                txtZDOpinion.Enabled = false;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (Save())
                {
                    ScriptHelper.Window.Alert("保存成功！", "hideDiv();");
                    BindControl();
                    BindWF();
                    Bind();
                }
                else
                {
                    ScriptHelper.Window.Alert("保存失败！", "hideDiv();");
                }
            }
            catch (Exception ex)
            {
                ScriptHelper.Window.Alert("保存失败！" + ex.Message, "hideDiv();");
            }
        }

        private bool Save()
        {
            bool bSave = new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).SaveAuditTable(
               Request.QueryString["ID"], hidBuStatus.Value, Convert.ToDecimal(numCurAmount.Text), Convert.ToDecimal(numCurAmount.Text), Convert.ToDecimal(numTAmount_C_A.Text), Convert.ToDecimal(numAmount_C.Text),
            rblPD1.SelectedValue == "1", rblPD2.SelectedValue == "1", rblPD3.SelectedValue == "1", rblPD4.SelectedValue == "1", rblPD5.SelectedValue == "1", rblPD6.SelectedValue == "1", rblPD7.SelectedValue == "1", rblPD8.SelectedValue == "1", rblPD9.SelectedValue == "1", rblPD10.SelectedValue == "1",
            txtPD1.Text, txtPD1Opinion.Text, txtPD2.Text, txtPD2Opinion.Text,
            rblCD1.SelectedValue == "1", rblCD2.SelectedValue == "1", rblCD3.SelectedValue == "1", rblCD4.SelectedValue == "1", rblCD5.SelectedValue == "1", rblCD6.SelectedValue == "1", rblCD7.SelectedValue == "1", rblCD8.SelectedValue == "1", rblCD9.SelectedValue == "1",
            txtCD.Text, txtCDOpinion.Text,
            rblZD1.SelectedValue == "1", rblZD2.SelectedValue == "1", rblZD3.SelectedValue == "1", rblZD4.SelectedValue == "1",
            txtZD.Text, txtZDOpinion.Text,
            txtForPPriceOpinion.Text, txtForPlanOpinion.Text,
            txtForLoanOpinion.Text, txtForCPriceOpinion.Text,
            txtForProgress.Text, txtForProgressOpinion.Text);
            return bSave;
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                if (hidBuStatus.Value == ((int)EnumItem.AuditTableStatus.FAppr).ToString()
                    || hidBuStatus.Value == ((int)EnumItem.AuditTableStatus.SAppr).ToString())
                {
                    Save();
                }
                Response.Redirect("Bu_AuditTable_Print.aspx?ID=" + Request.QueryString["ID"] + "&FunctionID=" + FunctionID);
            }
            catch (Exception ex)
            {
                ScriptHelper.Window.Alert("保存失败！" + ex.Message);
            }
        }
    }
}